﻿<html>
<head>
</head>
<body>
<div id="changelog"></div>
<script type="text/javascript" src="jquery-1.7.1-min.js"></script>
<script type="text/javascript">
localStorage['ksprime-split_projects'] = false;

if (typeof(localStorage.statii) == 'undefined')
{
	statii = {SHIPPED:'SHIPPED', RECEIVED:'RECEIVED', FULFILLED:'FULFILLED'};
	localStorage.setItem('statii', JSON.stringify(statii));
}

var version;
function getVersion(callback)
{
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open('GET', 'manifest.json');
	xmlhttp.onload = function (e)
	{
		var manifest = JSON.parse(xmlhttp.responseText);
		version = manifest.version;
	}
	xmlhttp.send(null);
}
getVersion();

// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Called when the url of a tab changes.
var tab_id = null;
function checkForValidUrl(tabId, changeInfo, tab) {
	if (tab.url.indexOf('kickstarter.com') > -1) {
		// ... show the page action.
		tab_id = tabId;
		chrome.pageAction.show(tabId);
	}
};

// Listen for any changes to the URL of any tab.
chrome.tabs.onUpdated.addListener(checkForValidUrl);

var lastCheck = {cached:true, updated:0, versions:[]};
chrome.extension.onRequest.addListener(function(request, sender, sendResponse){
	if(!request.action)
	return;

	switch(request.action)
	{
		case 'setOption':
		{
			//console.log(request.action +': '+ request.option +' = '+ request.value);
			if(request.option == null)
			{
				localStorage[request.option] = null;
				project = null;
				sendResponse();
				break;
			}

			//console.log('setting: '+ request.option +' to: '+ request.value);

			localStorage[request.option] = JSON.stringify(request.value);
			sendResponse(request.value);
			break;
		}

		case 'getOption':
		{
			if(localStorage[request.option])
			{
				value = JSON.parse(localStorage[request.option]);
			}
			else
			{
				value = request.status.replace('!', '');
			}
			sendResponse(value);
			//console.log(request.action +': '+ request.option +' = '+ value);
			break;
		}

		case 'checkUpdate':
		{
			//		  sec  min  hour
			var TTL = 60 * 60 * 24;// = 1 day time to expire
			var time = new Date().getTime();

			if (localStorage.KPupdate !== null)
			{
				lastCheck = JSON.parse(localStorage.KPupdate);
			}

			if(lastCheck == null || lastCheck.updated == 0 || lastCheck.updated + (TTL*1000) < time)
			{
				lastCheck = {cached: false};
				var xhr = new XMLHttpRequest();
				xhr.open("GET", "http://code.google.com/p/kickstarter-prime/wiki/Changelog?utm_source=extension&utm_campaign=kickstarter-prime&utm_medium=updatecheck", true);
				xhr.onreadystatechange = function(){
					if (xhr.readyState == 4){
						lastCheck.updated = time;

						lastCheck.versions=[];
						$(xhr.responseText.replace(/img|script/gi, 'scraped')).find('#wikicontent h3').each(function(){
						    lastCheck.versions.push($(this).text());
						});
						localStorage.KPupdate = JSON.stringify(lastCheck);
					}
				}
				xhr.send();
			}

			latestVersion = '';
			if (lastCheck.versions)
			{
				latest = version2number(lastCheck.versions[0]);
				myVersion = version2number(version);

				if (myVersion < latest)
				{
					updateText = '<a class="update" href="http://code.google.com/p/kickstarter-prime/downloads/list?utm_source=extension&utm_campaign=kickstarter-prime&utm_medium=update">UPDATE AVAILABLE: <span class="version">'+ lastCheck.versions[0] +'</span></a>';
					chrome.pageAction.setIcon({tabId: tab_id, path: 'images/update_icon_16.png'})
					sendResponse({'text': updateText, 'version': version});
					break;
				}
			}
			sendResponse({'version': version});
			break;
		}

		case 'addStatus':
		{
			statii = JSON.parse(localStorage.statii);
			statii[request.status] = request.status;
			localStorage.statii = JSON.stringify(statii);
			break;
		}

		case 'removeStatus':
		{
			statii = JSON.parse(localStorage.statii);
			delete statii[request.status];
			localStorage.setItem('statii', JSON.stringify(statii));
			break;
		}

		case 'getStatii':
		{
			sendResponse({'statii': JSON.parse(localStorage.statii)});
			break;
		}

		case 'getDropdown':
			dropdown = '<div class="status_dropdown"><span class="arrow">▼</span><a href="javascript:void(0);">FUNDED</a>';
			statii = JSON.parse(localStorage.statii);
			for (var i in statii)
			{
				dropdown += '<a href="javascript:void(0);">'+ i +'</a>';
			}
			dropdown += '</div>';
			sendResponse({'dropdown': dropdown});
		break;
	}
});

function version2number(version)
{
	number = '';
	v = version.replace(/v/gi, '').split('.');
	for(var i in v)
	{
		number += (v[i].length == 1) ? '0'+ v[i] : ''+v[i];
	}
	return parseFloat(number);
}

</script>
</body>
</html>